﻿/**  版本信息模板在安装目录下，可自行修改。
* Orders.cs
*
* 功 能： N/A
* 类 名： Orders
*
* Ver    变更日期             负责人  变更内容
* ───────────────────────────────────
* V0.01  2014/3/7 13:58:25   N/A    初版
*
* Copyright (c) 2012 Maticsoft Corporation. All rights reserved.
*┌──────────────────────────────────┐
*│　此技术信息为本公司机密信息，未经本公司书面同意禁止向第三方披露．　│
*│　版权所有：动软卓越（北京）科技有限公司　　　　　　　　　　　　　　│
*└──────────────────────────────────┘
*/
using System;
using System.Data;
using System.Text;
using System.Data.SqlClient;
using Maticsoft.DBUtility;//Please add references
namespace BBPA.DAL
{
	/// <summary>
	/// 数据访问类:Orders
	/// </summary>
	public partial class Orders
	{
		public Orders()
		{}
		#region  BasicMethod

		/// <summary>
		/// 得到最大ID
		/// </summary>
		public int GetMaxId()
		{
		return DbHelperSQL.GetMaxID("id", "Orders"); 
		}

		/// <summary>
		/// 是否存在该记录
		/// </summary>
		public bool Exists(int id)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) from Orders");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			return DbHelperSQL.Exists(strSql.ToString(),parameters);
		}


		/// <summary>
		/// 增加一条数据
		/// </summary>
		public int Add(BBPA.Model.Orders model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("insert into Orders(");
            strSql.Append("Number,StoreID,State,Dateandtime,ExpectPriceSum,ExpectBuyPriceSum,ExpectPay_Delivery,ExpectPay,ActualPriceSum,ActualBuyPriceSum,ActualPay_Delivery,ActualPay,IsBuyMoneyOK_Delivery,IsBuyMoneyOK,ExpressPrice,ExpressID,ExpressNumber,DateandtimeSend,BuyerID,SenderID,FirstExpectBuyDate,HasPay_Delivery,HasPay,gbid,ischeck)");
			strSql.Append(" values (");
            strSql.Append("@Number,@StoreID,@State,@Dateandtime,@ExpectPriceSum,@ExpectBuyPriceSum,@ExpectPay_Delivery,@ExpectPay,@ActualPriceSum,@ActualBuyPriceSum,@ActualPay_Delivery,@ActualPay,@IsBuyMoneyOK_Delivery,@IsBuyMoneyOK,@ExpressPrice,@ExpressID,@ExpressNumber,@DateandtimeSend,@BuyerID,@SenderID,@FirstExpectBuyDate,@HasPay_Delivery,@HasPay,@gbid,0)");
			strSql.Append(";select @@IDENTITY");
			SqlParameter[] parameters = {
					new SqlParameter("@Number", SqlDbType.VarChar,50),
					new SqlParameter("@StoreID", SqlDbType.Int,4),
					new SqlParameter("@State", SqlDbType.TinyInt,1),
					new SqlParameter("@Dateandtime", SqlDbType.DateTime),
					new SqlParameter("@ExpectPriceSum", SqlDbType.Money,8),
					new SqlParameter("@ExpectBuyPriceSum", SqlDbType.Money,8),
					new SqlParameter("@ExpectPay_Delivery", SqlDbType.Money,8),
					new SqlParameter("@ExpectPay", SqlDbType.Money,8),
					new SqlParameter("@ActualPriceSum", SqlDbType.Money,8),
					new SqlParameter("@ActualBuyPriceSum", SqlDbType.Money,8),
					new SqlParameter("@ActualPay_Delivery", SqlDbType.Money,8),
					new SqlParameter("@ActualPay", SqlDbType.Money,8),
					new SqlParameter("@IsBuyMoneyOK_Delivery", SqlDbType.Bit,1),
					new SqlParameter("@IsBuyMoneyOK", SqlDbType.Bit,1),
					new SqlParameter("@ExpressPrice", SqlDbType.Money,8),
					new SqlParameter("@ExpressID", SqlDbType.Int,4),
					new SqlParameter("@ExpressNumber", SqlDbType.VarChar,50),
					new SqlParameter("@DateandtimeSend", SqlDbType.DateTime),
					new SqlParameter("@BuyerID", SqlDbType.Int,4),
					new SqlParameter("@SenderID", SqlDbType.Int,4),
					new SqlParameter("@FirstExpectBuyDate", SqlDbType.Date,3),
					new SqlParameter("@HasPay_Delivery", SqlDbType.Bit,1),
					new SqlParameter("@HasPay", SqlDbType.Bit,1),new SqlParameter("@gbid", SqlDbType.Int,6)};
			parameters[0].Value = model.Number;
			parameters[1].Value = model.StoreID;
			parameters[2].Value = model.State;
			parameters[3].Value = model.Dateandtime;
			parameters[4].Value = model.ExpectPriceSum;
			parameters[5].Value = model.ExpectBuyPriceSum;
			parameters[6].Value = model.ExpectPay_Delivery;
			parameters[7].Value = model.ExpectPay;
			parameters[8].Value = model.ActualPriceSum;
			parameters[9].Value = model.ActualBuyPriceSum;
			parameters[10].Value = model.ActualPay_Delivery;
			parameters[11].Value = model.ActualPay;
			parameters[12].Value = model.IsBuyMoneyOK_Delivery;
			parameters[13].Value = model.IsBuyMoneyOK;
			parameters[14].Value = model.ExpressPrice;
			parameters[15].Value = model.ExpressID;
			parameters[16].Value = model.ExpressNumber;
			parameters[17].Value = model.DateandtimeSend;
			parameters[18].Value = model.BuyerID;
			parameters[19].Value = model.SenderID;
			parameters[20].Value = model.FirstExpectBuyDate;
			parameters[21].Value = model.HasPay_Delivery;
			parameters[22].Value = model.HasPay;
            parameters[23].Value = model.Gbid;

			object obj = DbHelperSQL.GetSingle(strSql.ToString(),parameters);
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 更新一条数据
		/// </summary>
		public bool Update(BBPA.Model.Orders model)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("update Orders set ");
			strSql.Append("Number=@Number,");
			strSql.Append("StoreID=@StoreID,");
			strSql.Append("State=@State,");
			strSql.Append("Dateandtime=@Dateandtime,");
			strSql.Append("ExpectPriceSum=@ExpectPriceSum,");
			strSql.Append("ExpectBuyPriceSum=@ExpectBuyPriceSum,");
			strSql.Append("ExpectPay_Delivery=@ExpectPay_Delivery,");
			strSql.Append("ExpectPay=@ExpectPay,");
			strSql.Append("ActualPriceSum=@ActualPriceSum,");
			strSql.Append("ActualBuyPriceSum=@ActualBuyPriceSum,");
			strSql.Append("ActualPay_Delivery=@ActualPay_Delivery,");
			strSql.Append("ActualPay=@ActualPay,");
			strSql.Append("IsBuyMoneyOK_Delivery=@IsBuyMoneyOK_Delivery,");
			strSql.Append("IsBuyMoneyOK=@IsBuyMoneyOK,");
			strSql.Append("ExpressPrice=@ExpressPrice,");
			strSql.Append("ExpressID=@ExpressID,");
			strSql.Append("ExpressNumber=@ExpressNumber,");
			strSql.Append("DateandtimeSend=@DateandtimeSend,");
			strSql.Append("BuyerID=@BuyerID,");
			strSql.Append("SenderID=@SenderID,");
			strSql.Append("FirstExpectBuyDate=@FirstExpectBuyDate,");
			strSql.Append("HasPay_Delivery=@HasPay_Delivery,");
			strSql.Append("HasPay=@HasPay,");
            strSql.Append("ischeck=@ischeck,");
            strSql.Append("GetMoney=@GetMoney");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@Number", SqlDbType.VarChar,50),
					new SqlParameter("@StoreID", SqlDbType.Int,4),
					new SqlParameter("@State", SqlDbType.TinyInt,1),
					new SqlParameter("@Dateandtime", SqlDbType.DateTime),
					new SqlParameter("@ExpectPriceSum", SqlDbType.Money,8),
					new SqlParameter("@ExpectBuyPriceSum", SqlDbType.Money,8),
					new SqlParameter("@ExpectPay_Delivery", SqlDbType.Money,8),
					new SqlParameter("@ExpectPay", SqlDbType.Money,8),
					new SqlParameter("@ActualPriceSum", SqlDbType.Money,8),
					new SqlParameter("@ActualBuyPriceSum", SqlDbType.Money,8),
					new SqlParameter("@ActualPay_Delivery", SqlDbType.Money,8),
					new SqlParameter("@ActualPay", SqlDbType.Money,8),
					new SqlParameter("@IsBuyMoneyOK_Delivery", SqlDbType.Bit,1),
					new SqlParameter("@IsBuyMoneyOK", SqlDbType.Bit,1),
					new SqlParameter("@ExpressPrice", SqlDbType.Money,8),
					new SqlParameter("@ExpressID", SqlDbType.Int,4),
					new SqlParameter("@ExpressNumber", SqlDbType.VarChar,50),
					new SqlParameter("@DateandtimeSend", SqlDbType.DateTime),
					new SqlParameter("@BuyerID", SqlDbType.Int,4),
					new SqlParameter("@SenderID", SqlDbType.Int,4),
					new SqlParameter("@FirstExpectBuyDate", SqlDbType.Date,3),
					new SqlParameter("@HasPay_Delivery", SqlDbType.Bit,1),
					new SqlParameter("@HasPay", SqlDbType.Bit,1),
                    new SqlParameter("@ischeck", SqlDbType.Int,1),
                    new SqlParameter("@GetMoney", SqlDbType.Money,8),
					new SqlParameter("@id", SqlDbType.Int,4)};
			parameters[0].Value = model.Number;
			parameters[1].Value = model.StoreID;
			parameters[2].Value = model.State;
			parameters[3].Value = model.Dateandtime;
			parameters[4].Value = model.ExpectPriceSum;
			parameters[5].Value = model.ExpectBuyPriceSum;
			parameters[6].Value = model.ExpectPay_Delivery;
			parameters[7].Value = model.ExpectPay;
			parameters[8].Value = model.ActualPriceSum;
			parameters[9].Value = model.ActualBuyPriceSum;
			parameters[10].Value = model.ActualPay_Delivery;
			parameters[11].Value = model.ActualPay;
			parameters[12].Value = model.IsBuyMoneyOK_Delivery;
			parameters[13].Value = model.IsBuyMoneyOK;
			parameters[14].Value = model.ExpressPrice;
			parameters[15].Value = model.ExpressID;
			parameters[16].Value = model.ExpressNumber;
			parameters[17].Value = model.DateandtimeSend;
			parameters[18].Value = model.BuyerID;
			parameters[19].Value = model.SenderID;
			parameters[20].Value = model.FirstExpectBuyDate;
			parameters[21].Value = model.HasPay_Delivery;
			parameters[22].Value = model.HasPay;
            parameters[23].Value = model.Ischeck;
            parameters[24].Value = model.Getmoney;
			parameters[25].Value = model.id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}

		/// <summary>
		/// 删除一条数据
		/// </summary>
		public bool Delete(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Orders ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			int rows=DbHelperSQL.ExecuteSql(strSql.ToString(),parameters);
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}
		/// <summary>
		/// 批量删除数据
		/// </summary>
		public bool DeleteList(string idlist )
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("delete from Orders ");
			strSql.Append(" where id in ("+idlist + ")  ");
			int rows=DbHelperSQL.ExecuteSql(strSql.ToString());
			if (rows > 0)
			{
				return true;
			}
			else
			{
				return false;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public BBPA.Model.Orders GetModel(int id)
		{
			
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select  top 1 id,Number,StoreID,State,Dateandtime,ExpectPriceSum,ExpectBuyPriceSum,ExpectPay_Delivery,ExpectPay,ActualPriceSum,ActualBuyPriceSum,ActualPay_Delivery,ActualPay,IsBuyMoneyOK_Delivery,IsBuyMoneyOK,ExpressPrice,ExpressID,ExpressNumber,DateandtimeSend,BuyerID,SenderID,FirstExpectBuyDate,HasPay_Delivery,HasPay,gbid,ischeck,GetMoney from Orders ");
			strSql.Append(" where id=@id");
			SqlParameter[] parameters = {
					new SqlParameter("@id", SqlDbType.Int,4)
			};
			parameters[0].Value = id;

			BBPA.Model.Orders model=new BBPA.Model.Orders();
			DataSet ds=DbHelperSQL.Query(strSql.ToString(),parameters);
			if(ds.Tables[0].Rows.Count>0)
			{
				return DataRowToModel(ds.Tables[0].Rows[0]);
			}
			else
			{
				return null;
			}
		}


		/// <summary>
		/// 得到一个对象实体
		/// </summary>
		public BBPA.Model.Orders DataRowToModel(DataRow row)
		{
			BBPA.Model.Orders model=new BBPA.Model.Orders();
			if (row != null)
			{
				if(row["id"]!=null && row["id"].ToString()!="")
				{
					model.id=int.Parse(row["id"].ToString());
				}
				if(row["Number"]!=null)
				{
					model.Number=row["Number"].ToString();
				}
				if(row["StoreID"]!=null && row["StoreID"].ToString()!="")
				{
					model.StoreID=int.Parse(row["StoreID"].ToString());
				}
				if(row["State"]!=null && row["State"].ToString()!="")
				{
					model.State=int.Parse(row["State"].ToString());
				}
				if(row["Dateandtime"]!=null && row["Dateandtime"].ToString()!="")
				{
					model.Dateandtime=DateTime.Parse(row["Dateandtime"].ToString());
				}
				if(row["ExpectPriceSum"]!=null && row["ExpectPriceSum"].ToString()!="")
				{
					model.ExpectPriceSum=decimal.Parse(row["ExpectPriceSum"].ToString());
				}
				if(row["ExpectBuyPriceSum"]!=null && row["ExpectBuyPriceSum"].ToString()!="")
				{
					model.ExpectBuyPriceSum=decimal.Parse(row["ExpectBuyPriceSum"].ToString());
				}
				if(row["ExpectPay_Delivery"]!=null && row["ExpectPay_Delivery"].ToString()!="")
				{
					model.ExpectPay_Delivery=decimal.Parse(row["ExpectPay_Delivery"].ToString());
				}
				if(row["ExpectPay"]!=null && row["ExpectPay"].ToString()!="")
				{
					model.ExpectPay=decimal.Parse(row["ExpectPay"].ToString());
				}
				if(row["ActualPriceSum"]!=null && row["ActualPriceSum"].ToString()!="")
				{
					model.ActualPriceSum=decimal.Parse(row["ActualPriceSum"].ToString());
				}
				if(row["ActualBuyPriceSum"]!=null && row["ActualBuyPriceSum"].ToString()!="")
				{
					model.ActualBuyPriceSum=decimal.Parse(row["ActualBuyPriceSum"].ToString());
				}
				if(row["ActualPay_Delivery"]!=null && row["ActualPay_Delivery"].ToString()!="")
				{
					model.ActualPay_Delivery=decimal.Parse(row["ActualPay_Delivery"].ToString());
				}
				if(row["ActualPay"]!=null && row["ActualPay"].ToString()!="")
				{
					model.ActualPay=decimal.Parse(row["ActualPay"].ToString());
				}
				if(row["IsBuyMoneyOK_Delivery"]!=null && row["IsBuyMoneyOK_Delivery"].ToString()!="")
				{
					if((row["IsBuyMoneyOK_Delivery"].ToString()=="1")||(row["IsBuyMoneyOK_Delivery"].ToString().ToLower()=="true"))
					{
						model.IsBuyMoneyOK_Delivery=true;
					}
					else
					{
						model.IsBuyMoneyOK_Delivery=false;
					}
				}
				if(row["IsBuyMoneyOK"]!=null && row["IsBuyMoneyOK"].ToString()!="")
				{
					if((row["IsBuyMoneyOK"].ToString()=="1")||(row["IsBuyMoneyOK"].ToString().ToLower()=="true"))
					{
						model.IsBuyMoneyOK=true;
					}
					else
					{
						model.IsBuyMoneyOK=false;
					}
				}
				if(row["ExpressPrice"]!=null && row["ExpressPrice"].ToString()!="")
				{
					model.ExpressPrice=decimal.Parse(row["ExpressPrice"].ToString());
				}
				if(row["ExpressID"]!=null && row["ExpressID"].ToString()!="")
				{
					model.ExpressID=int.Parse(row["ExpressID"].ToString());
				}
				if(row["ExpressNumber"]!=null)
				{
					model.ExpressNumber=row["ExpressNumber"].ToString();
				}
				if(row["DateandtimeSend"]!=null && row["DateandtimeSend"].ToString()!="")
				{
					model.DateandtimeSend=DateTime.Parse(row["DateandtimeSend"].ToString());
				}
				if(row["BuyerID"]!=null && row["BuyerID"].ToString()!="")
				{
					model.BuyerID=int.Parse(row["BuyerID"].ToString());
				}
				if(row["SenderID"]!=null && row["SenderID"].ToString()!="")
				{
					model.SenderID=int.Parse(row["SenderID"].ToString());
				}
				if(row["FirstExpectBuyDate"]!=null && row["FirstExpectBuyDate"].ToString()!="")
				{
					model.FirstExpectBuyDate=DateTime.Parse(row["FirstExpectBuyDate"].ToString());
				}
				if(row["HasPay_Delivery"]!=null && row["HasPay_Delivery"].ToString()!="")
				{
					if((row["HasPay_Delivery"].ToString()=="1")||(row["HasPay_Delivery"].ToString().ToLower()=="true"))
					{
						model.HasPay_Delivery=true;
					}
					else
					{
						model.HasPay_Delivery=false;
					}
				}
				if(row["HasPay"]!=null && row["HasPay"].ToString()!="")
				{
					if((row["HasPay"].ToString()=="1")||(row["HasPay"].ToString().ToLower()=="true"))
					{
						model.HasPay=true;
					}
					else
					{
						model.HasPay=false;
					}
				}
                if (row["gbid"] != null && row["gbid"].ToString() != "")
                {
                    model.Gbid = int.Parse(row["gbid"].ToString());
                }
                if (row["ischeck"] != null && row["ischeck"].ToString() != "")
                {
                    model.Ischeck = int.Parse(row["ischeck"].ToString());
                }
                if (row["GetMoney"] != null && row["GetMoney"].ToString() != "")
                {
                    model.Getmoney = Convert.ToDecimal(row["GetMoney"].ToString());
                }
			}
			return model;
		}

		/// <summary>
		/// 获得数据列表
		/// </summary>
		public DataSet GetList(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
            strSql.Append("select id,Number,StoreID,State,Dateandtime,ExpectPriceSum,ExpectBuyPriceSum,ExpectPay_Delivery,ExpectPay,ActualPriceSum,ActualBuyPriceSum,ActualPay_Delivery,ActualPay,IsBuyMoneyOK_Delivery,IsBuyMoneyOK,ExpressPrice,ExpressID,ExpressNumber,DateandtimeSend,BuyerID,SenderID,FirstExpectBuyDate,HasPay_Delivery,HasPay,gbid,ischeck,GetMoney ");
			strSql.Append(" FROM Orders ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获得前几行数据
		/// </summary>
		public DataSet GetList(int Top,string strWhere,string filedOrder)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select ");
			if(Top>0)
			{
				strSql.Append(" top "+Top.ToString());
			}
            strSql.Append(" id,Number,StoreID,State,Dateandtime,ExpectPriceSum,ExpectBuyPriceSum,ExpectPay_Delivery,ExpectPay,ActualPriceSum,ActualBuyPriceSum,ActualPay_Delivery,ActualPay,IsBuyMoneyOK_Delivery,IsBuyMoneyOK,ExpressPrice,ExpressID,ExpressNumber,DateandtimeSend,BuyerID,SenderID,FirstExpectBuyDate,HasPay_Delivery,HasPay,gbid,ischeck,GetMoney ");
			strSql.Append(" FROM Orders ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			strSql.Append(" order by " + filedOrder);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/// <summary>
		/// 获取记录总数
		/// </summary>
		public int GetRecordCount(string strWhere)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("select count(1) FROM Orders ");
			if(strWhere.Trim()!="")
			{
				strSql.Append(" where "+strWhere);
			}
			object obj = DbHelperSQL.GetSingle(strSql.ToString());
			if (obj == null)
			{
				return 0;
			}
			else
			{
				return Convert.ToInt32(obj);
			}
		}
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
		{
			StringBuilder strSql=new StringBuilder();
			strSql.Append("SELECT * FROM ( ");
			strSql.Append(" SELECT ROW_NUMBER() OVER (");
			if (!string.IsNullOrEmpty(orderby.Trim()))
			{
				strSql.Append("order by T." + orderby );
			}
			else
			{
				strSql.Append("order by T.id desc");
			}
			strSql.Append(")AS Row, T.*  from Orders T ");
			if (!string.IsNullOrEmpty(strWhere.Trim()))
			{
				strSql.Append(" WHERE " + strWhere);
			}
			strSql.Append(" ) TT");
			strSql.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
			return DbHelperSQL.Query(strSql.ToString());
		}

		/*
		/// <summary>
		/// 分页获取数据列表
		/// </summary>
		public DataSet GetList(int PageSize,int PageIndex,string strWhere)
		{
			SqlParameter[] parameters = {
					new SqlParameter("@tblName", SqlDbType.VarChar, 255),
					new SqlParameter("@fldName", SqlDbType.VarChar, 255),
					new SqlParameter("@PageSize", SqlDbType.Int),
					new SqlParameter("@PageIndex", SqlDbType.Int),
					new SqlParameter("@IsReCount", SqlDbType.Bit),
					new SqlParameter("@OrderType", SqlDbType.Bit),
					new SqlParameter("@strWhere", SqlDbType.VarChar,1000),
					};
			parameters[0].Value = "Orders";
			parameters[1].Value = "id";
			parameters[2].Value = PageSize;
			parameters[3].Value = PageIndex;
			parameters[4].Value = 0;
			parameters[5].Value = 0;
			parameters[6].Value = strWhere;	
			return DbHelperSQL.RunProcedure("UP_GetRecordByPage",parameters,"ds");
		}*/

		#endregion  BasicMethod
		#region  ExtensionMethod

		#endregion  ExtensionMethod
	}
}

